import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children: [
      {
        path: '/',
        name: 'Demo',
        component: () => import('../views/Demo.vue')
      },
      {
        path: '/echarts',
        name: "echarts",
        component: () => import('../views/Echarts.vue'),
      },
      {
        path: '/manage',
        name: 'manage',
        component: () => import('../views/Manage.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  var a = localStorage.getItem('token')
  if (to.name === 'login') {
    next()
  } else {
    if (a) {
      next()
    } else {
      next('login')
    }
  }
})

export default router
